package org.itzheng.yuezhi.real.main;

import android.content.Context;
import android.graphics.Color;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import org.itzheng.yuezhi.R;
import org.itzheng.yuezhi.adapter.BaseFragmentPagerAdapter;
import org.itzheng.yuezhi.adapter.BaseRecyclerViewAdapter;
import org.itzheng.yuezhi.base.BaseFragment;
import org.itzheng.yuezhi.constant.HttpUrl;
import org.itzheng.yuezhi.constant.SpKey;
import org.itzheng.yuezhi.entity.WeChatArticleCategoryEntity;
import org.itzheng.yuezhi.fragment.WeChatArticleContentFragment;
import org.itzheng.yuezhi.utils.JsonUtils;
import org.itzheng.yuezhi.utils.MyOkHttp;
import org.itzheng.yuezhi.utils.MySP;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * Title:<br>
 * Description: <br>
 * Company: <br>
 *
 * @author ZhengYongdong
 * @email ItZheng@ZoHo.com
 * @date 2016/6/25 0025
 */
public class FindFragment extends BaseFragment {
    @Override
    protected View onCreateView(LayoutInflater inflater, ViewGroup container) {
        return View.inflate(getActivity(), R.layout.fragment_find,null);
    }

    /**
     * Title:微信文章推荐<br>
     * Description: <br>
     * Company: <br>
     *
     * @author ZhengYongdong
     * @email ItZheng@ZoHo.com
     * @date 2016/6/24 0024
     */
    public static class ReadFragment extends BaseFragment {
        View rootView;
        RecyclerView rcv_wx_article_category;
        ViewPager rcv_wx_article_content;
        ArrayList<Fragment> fragments = new ArrayList<>();
        ArrayList<WeChatArticleCategoryEntity.Result> categories;
        RecyclerViewAdapter recyclerViewAdapter;

        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container) {
            if (rootView == null) {
                rootView = View.inflate(getActivity(), R.layout.fragment_read, null);
                initData();
            }
            return rootView;
        }

        private void initView() {
            setRecycleView();
            setContentViewPager();
        }

        private void setContentViewPager() {
            rcv_wx_article_content = (ViewPager) rootView.findViewById(R.id.rcv_wx_article_content);
            rcv_wx_article_content.setAdapter(new BaseFragmentPagerAdapter(getChildFragmentManager(), fragments));
            rcv_wx_article_content.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                @Override
                public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                }

                @Override
                public void onPageSelected(int position) {
                    recyclerViewAdapter.setSelection(position);
                }

                @Override
                public void onPageScrollStateChanged(int state) {

                }
            });
        }

        private void setRecycleView() {
            rcv_wx_article_category = (RecyclerView) rootView.findViewById(R.id.rcv_wx_article_category);
            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
            linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
            rcv_wx_article_category.setLayoutManager(linearLayoutManager);
            recyclerViewAdapter = new RecyclerViewAdapter(getActivity(), rcv_wx_article_category, categories);
            rcv_wx_article_category.setAdapter(recyclerViewAdapter);
            recyclerViewAdapter.setOnItemClickListener(new BaseRecyclerViewAdapter.OnItemClickListener() {
                @Override
                public void OnItemClick(View view, int position) {
                    rcv_wx_article_content.setCurrentItem(position);
                }
            });
        }

        private void initData() {
            String category = MySP.getInstance().getString(SpKey.WX_ARTICLE_CATEGORY, null);
            if (TextUtils.isEmpty(category)) {
                getNetData();
            } else {
                parseJson(category);
            }
        }

        private void getNetData() {
            HashMap<String, Object> hashMap = new HashMap();
            hashMap.put("key", HttpUrl.MOB_API_KEY);
            MyOkHttp.getInstance().getUrl(HttpUrl.WX_ARTICLE_CATEGORY, hashMap, new MyOkHttp.RequestCallBack() {
                @Override
                public void onFailure(Exception httpException, String describe) {

                }

                @Override
                public void onSuccess(String result) {
                    parseJson(result);
                }
            });
        }

        private void parseJson(String result) {
            WeChatArticleCategoryEntity weChatArticleCategoryEntity
                    = JsonUtils.fromJson(result, WeChatArticleCategoryEntity.class);
            if (weChatArticleCategoryEntity == null || weChatArticleCategoryEntity.getResult() == null) {
            } else {
                categories = (ArrayList<WeChatArticleCategoryEntity.Result>) weChatArticleCategoryEntity.getResult();
                MySP.getInstance().putString(SpKey.WX_ARTICLE_CATEGORY, result);
                fragments.clear();
                for (int i = 0; i < categories.size(); i++) {
                    WeChatArticleCategoryEntity.Result category = categories.get(i);
                    fragments.add(new WeChatArticleContentFragment(category.getCid(), category.getName()));
                }
            }
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    initView();
                }
            });
        }

        class RecyclerViewAdapter extends BaseRecyclerViewAdapter<RecyclerViewAdapter.MyHolder> {
            int mSelection = 0;
            Context mContext;
            ArrayList<WeChatArticleCategoryEntity.Result> mTitles;
            RecyclerView mRecyclerView;

            public RecyclerViewAdapter(FragmentActivity context, RecyclerView recyclerView, ArrayList<WeChatArticleCategoryEntity.Result> titles) {
                mContext = context;
                mRecyclerView = recyclerView;
                mTitles = titles;
            }

            @Override
            public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                View view = View.inflate(mContext, R.layout.item_wechat_article_category, null);
                MyHolder myHolder = new MyHolder(view);
                myHolder.tv_item_wechat_article_category_title = (TextView) view.findViewById(R.id.tv_item_wechat_article_category_title);
                myHolder.tv_item_wechat_article_category_title.setTextSize(18);

                return myHolder;
            }


            public void setSelection(int selection) {
                mSelection = selection;
                mRecyclerView.scrollToPosition(selection);
                this.notifyDataSetChanged();
            }

            @Override
            public int getItemCount() {
                if (mTitles == null) {
                    return 0;
                }
                return mTitles.size();
            }

            @Override
            public void onBindViewHolder(MyHolder holder, int position, boolean isBaseRecyclerViewAdapter) {
                if (position == mSelection) {
                    holder.tv_item_wechat_article_category_title.setTextColor(Color.RED);
                } else {
                    holder.tv_item_wechat_article_category_title.setTextColor(Color.BLACK);
                }
                holder.tv_item_wechat_article_category_title.setText(mTitles.get(position).getName().toString() + "  ");
            }

            class MyHolder extends RecyclerView.ViewHolder {
                TextView tv_item_wechat_article_category_title;

                public MyHolder(View itemView) {
                    super(itemView);
                }
            }
        }
    }
}
